package com.gjh.controller.system;

import com.github.pagehelper.PageInfo;
import com.gjh.controller.BeanController;

import com.gjh.domain.system.Module;
import com.gjh.service.system.ModuleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BeanController{

    @Autowired
    private ModuleService moduleService;

    @RequestMapping(value = "/list",name = "展示模块列表数据")
    public String findAll(@RequestParam(name ="page",defaultValue = "1")int pageNum,@RequestParam(name = "pageSize",defaultValue = "10")int pageSize){
        PageInfo page= moduleService.findByPage(pageNum,pageSize);
        request.setAttribute("page",page);
        return "system/module/module-list";
    }

    @RequestMapping(value = "/toAdd" ,name = "进入新增模块界面")
    public String toAdd(){
        //查询所有上级模块
        List<Module> moduleList = moduleService.findAll();
        request.setAttribute("menus",moduleList);
        return "system/module/module-add";
    }

    @RequestMapping(value = "/edit",name = "保存模块数据")
    public String edit(Module module){
        //判断module中是否有id
        if(StringUtils.isEmpty(module.getId())){
           module.setId(UUID.randomUUID().toString());
           moduleService.save(module);
        }else {
            moduleService.update(module);
        }
        return "redirect:/system/module/list.do";
    }

    @RequestMapping(value = "/toUpdate",name = "进入模块更新界面")
    public String toUpdate(String id){
        Module module=moduleService.findById(id);
        request.setAttribute("module",module);
        List<Module> moduleList = moduleService.findAll();
        request.setAttribute("menus",moduleList);
        return "system/module/module-update";
    }

    @RequestMapping(value = "/delete",name = "删除模块")
    public String deleteById(String id){
        moduleService.deleteById(id);
        return "redirect:/system/module/list.do";
    }


}
